<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use HasFactory;

    protected $table = 'sp_article';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //status状态   1已发布 2未发布
    const ARTICLE_STATUS_IS_RELEASE = 1;//已发布
    const ARTICLE_STATUS_NOT_RELEASE = 2;//未发布

    //is_delete是否删除  1是 2否
    const ARTICLE_IS_DELETE = 1;//是
    const ARTICLE_NOT_DELETE = 2;//否

    //pc_show是否显示在PC商城   1是  2否
    const ARTICLE_PC_IS_SHOW = 1;//是
    const ARTICLE_PC_NOT_SHOW = 2;//否

    //h5_show是否显示在h5商城   1是  2否
    const ARTICLE_H5_IS_SHOW = 1;//是
    const ARTICLE_H5_NOT_SHOW = 2;//否

    //xcx_show否显示在支付宝小程序   1是  2否
    const ARTICLE_XCX_IS_SHOW = 1;//是
    const ARTICLE_XCX_NOT_SHOW = 2;//否

    /**
     * 获取首页新闻动态
     */
    protected function getNewsDynamic()
    {
        $news = $this->where([['status', 1], ['is_delete', 2], ['home_recommend', 1], ['pc_show', 1]])
            ->select('id', 'article_title', 'update_time', 'publish_time')
            ->orderBy('update_time', 'desc')
            ->limit(3)
            ->get();
        $news = $news ? $news->toArray() : [];
        return $news;
    }

    //根据type_id获取文章信息
    protected function GetArticleByTypeid($pageSize, $type_id)
    {
        $select = array('id', 'type_id', 'parentid', 'author', 'article_title', 'article_img', 'abstract', 'content', 'home_recommend', 'hot_recommend', 'status', 'publish_time', 'is_delete');
        $where = array(
            'status' => 1, //状态   1已发布 2未发布
            'is_delete' => 2, //是否删除  1是 2否
            'type_id' => $type_id,
            'pc_show' => 1, //是否显示在PC商城   1是  2否
        );
        $data = $this
            ->select($select)
            ->where($where)
            ->orderBy('id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //热门资讯左侧栏
    protected function getHotNews()
    {
        $select = array('id', 'article_title', 'update_time', 'publish_time');
        $where = array(
            'status' => 1, //状态   1已发布 2未发布
            'is_delete' => 2, //是否删除  1是 2否
            'hot_recommend' => 1, //是否热门推荐  1是  2否
            'pc_show' => 1, //是否显示在PC商城   1是  2否
        );
        $data = $this
            ->select($select)
            ->where($where)
            ->orderBy('update_time', 'desc')
            ->limit(3)
            ->get();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //查询一条文章信息
    protected function GetOneArticle($where)
    {
        $select = array('id', 'type_id', 'parentid', 'author', 'article_title', 'article_img', 'abstract', 'content', 'home_recommend', 'hot_recommend', 'status', 'publish_time', 'is_delete');
        $data = $this
            ->where($where)
            ->select($select)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //添加文章信息
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //根据id查询文章信息
    protected function GetArticleById($id)
    {
        $where = array(
            'id' => $id,
        );
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //根据id修改文章信息
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }

    //平台文章列表
    protected function GetPlatformArticleList($pageSize, $where)
    {
        $select = array('a.id', 'a.type_id', 'a.parentid', 'a.article_title', 'at.type_name', 'a.author',
            'a.home_recommend', 'a.hot_recommend', 'a.status', 'a.publish_time', 'a.is_delete', 'a.pc_show', 'a.h5_show', 'a.xcx_show');
        $data = $this
            ->from('sp_article as a')
            ->select($select)
            ->leftJoin('sp_article_type as at', function ($join) {
                $join->on('at.id', '=', 'a.type_id')
                    ->on('at.parentid', '=', 'a.parentid');
            })
            ->where($where)
            ->orderBy('a.id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : [];
        return $data;
    }


}
